package cn.bob.org.controller;

import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Writer;
import cn.bob.org.query.WriterQuery;
import cn.bob.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/writer")
public class WriterController {

    @Autowired
    private WriterService writerService;

    /**
     * 查询全部
     */
    @GetMapping
    public List<Writer> findAllWriter(){

        return writerService.findAllWriter();
    }

    /**
     * 查询单条
     */
    @GetMapping("/{id}")
    public Writer findWriterById(@PathVariable("id") Long id){
        return writerService.findWriterById(id);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteWriter(@PathVariable("id") Long id){
        writerService.deleteWriter(id);
        return AjaxResult.me().success();
    }

    /**
     * 修改或添加
     */
    @PutMapping
    public AjaxResult addOrUpdateWriter(@RequestBody Writer writer){
        if (writer.getId() == null){
            writerService.addWriter(writer);
        }else {
            writerService.updateWriter(writer);
        }
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Writer> queryByPage(@RequestBody WriterQuery query){
        return writerService.queryByPage(query);
    }

}
